
var $table = $('#addTo-index-goods');

$(function () {
    //banner图图片地址
    initFileInput("file_upload", urlcore + "/api/attachment/upload", "#picUrl");
    
    selectedGroupType = $("#groupType").val();
    //创建待分组商品列表
    initUngroupedTable(selectedGroupType);
});



//更换产品线
$("#groupType").on("change", function () {
    selectedGroupType = $("#groupType").val();
    $("#curGroup").html("");
    // 先销毁原table
    $table.bootstrapTable("destroy");
    //创建更改产品线后的待分组商品列表
    initUngroupedTable(selectedGroupType);
});


// 初始化当前产品线待分组商品bootstrap-table
function initUngroupedTable(selectedGroupType) {
    // 初始化bootstrap-table
    var url=urlcore + "/api/experienceGoods/getNotGroupedGoodsList";
    /*    var params = {
        "goods": {
            goodsType: selectedGroupType,
        },
        "page": {
            current: 1,
        }
    };*/
    //init table params
    var params = {
        goodsType: selectedGroupType
    };
    initGroupTable(url,params);

}


//添加商品
function add(){
	var selection = $table.bootstrapTable('getSelections');
		//活动图片地址
    var picUrl = "";
    $("#picUrl img").each(function(index,item){
		picUrl += $(this).attr("src")+"***";
	});
	if(picUrl!=""){
		  picUrl = picUrl.substring(0, picUrl.length-3);
	}
	
    if (selection.length == 0) {
        alert("请先选择商品");
        return;
    }

    var price = $("#price").val();
    if(price == ""){
        alert("请输入雅币！");
        return;
    }
    var num = $("#num").val();
    if(num == ""){
        alert("请输入数量");
        return;
    }
    //当前分组id
    indexGroupId = $("#curGroup").val();

    var experienceGoods = [];
    for (var item of selection) {
        experienceGoods.push({
            goodsId: item.id,
            picUrl:picUrl,
            price:price,
            num:num
        })
    }
	
	    $.ajax({
        type: "POST",
        url: urlcore + "/api/experienceGoods/addBatch",
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(
        	  experienceGoods
        ),
        success: function (data) {
            alert("添加成功");
            //$table.bootstrapTable('refresh');
            location.reload();
        },
        error: function () {
            alert("失败");
        }
    });

}


//初始化表格，有管理已有商品和添加分组商品两类表格
function initGroupTable(url,params){
    $table.bootstrapTable({
        columns: [{
            field: "state",
            checkbox: true,
            title: "ID"
        }, {
            field: "id",
            title: "ID"
        }, {
            field: "goodsName",
            title: "商品名称"
        }, {
            field: "mPrice",
            title: "商品价格"
        }],
        method: "get",
        url:url ,
        dataType: "json",
        queryParams: params,
        // data:[{
        //     "id":1,
        //     "goodsName":"商品1"
        // },{
        //     "id":2,
        //     "goodsName":"商品2"
        // }],
        //返回数据预处理
        responseHandler: function (cbdata) {
            var res = cbdata.data;
            return res;
        },
        toolbar:'#addGroupToolbar',
        pagination: true,//启用分页条
        sidePagination: "client",
        // pageNumber: 1,
        pageSize: 16,//每页大小
        pageList: [16, 32, 48, 64],//可以选择每页大小
        showRefresh: true,
        sortable: true,//是否启用排序
        clickToSelect: true,//设置true 将在点击行时，自动选择rediobox 和 checkbox
        search: true,
        // searchText:"",//初始搜索文本

    });
    
    
}